/*
 * Copyright (C), 2002-2018, Caljm有限公司
 * FileName: Consumer
 * Author:   Byron Wang
 * Date:     2018/12/15 下午9:53
 * Description: //模块目的、功能描述
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */

package com.cpcs.mq.quickstart;

import com.cpcs.Constants;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * 功能描述：消费者
 *
 * @author Byron Wang
 * @since 2018/12/15
 */
public class Consumer {

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        factory.setVirtualHost(Constants.VIRTUAL_HOST);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        String exchangeName = "test_start_exchange";
        String queueName = "test_start_queue";
        String routingKey = "test.#";

        channel.exchangeDeclare(exchangeName, "direct", true, false, null);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);

        channel.basicConsume(queueName, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                 AMQP.BasicProperties properties, byte[] body) throws IOException
            {
                System.out.println("handleDelivery envelope:" + envelope);
            }
        });

    }
}
